package net.dreamlu.common.base;

import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;

import com.jfinal.plugin.activerecord.Record;
import net.dreamlu.vo.DataTables;

/**
 * 抽象公共控制器
 */
public abstract class BaseController extends Controller {
	protected final Log logger = Log.getLog(this.getClass());
	
	// index
	public void index(){}
	
	/**
	 * DataTable渲染
	 * @param page
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void renderDataTable(Page page) {
		int draw = getParaToInt("draw", 0);
		renderJson(new DataTables(draw, page));
	}

	/**
	 * ajax 错误信息
	 * @param msg 错误信息
	 */
	public void renderError(String msg) {
		Record record = new Record();
		record.set("success", false);
		record.set("msg", msg);
		renderJson(record);
	}

	/**
	 * ajax成功
	 */
	public void renderSuccess() {
		Record record = new Record();
		record.set("success", true);
		renderJson(record);
	}

	/**
	 * 简化操作
	 * @param succeed 是否成功
	 */
	public void renderSucceed(boolean succeed) {
		if (succeed) {
			renderSuccess();
		} else {
			renderError("操作失败！");
		}
	}

	/**
	 * ajax成功并携带数据
	 * @param data 携带的数据
	 */
	public void renderSuccess(Object data) {
		Record record = new Record();
		record.set("success", true);
		record.set("data", data);
		renderJson(record);
	}

}
